/*
 * Copyright 2011 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package fr.gaellalire.jzip.view;

import java.awt.Component;
import java.text.ParseException;

import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.TableCellRenderer;
import javax.swing.text.NumberFormatter;

/**
 *
 * @author Gael Lalire
 */
public class NoBackgroundTableCellRenderer extends JLabel implements TableCellRenderer {

    private static final long serialVersionUID = 2686249053138040707L;

    private NumberFormatter numberFormatter = new NumberFormatter();

    @Override
    public Component getTableCellRendererComponent(final JTable table, final Object value, final boolean isSelected, final boolean hasFocus, final int row, final int column) {
        if (value != null) {
            if (value instanceof Long || value instanceof Integer) {
                setHorizontalAlignment(SwingConstants.RIGHT);
                try {
                    setText(numberFormatter.valueToString(value));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            } else {
                setHorizontalAlignment(SwingConstants.LEFT);
                setText(value.toString());
            }
        } else {
            setText("");
        }
        setOpaque(true);
        setBackground(table.getBackground());
        setForeground(table.getForeground());
        return this;
    }

}
